#include <iostream>
#include <queue>

using namespace std;

int ntest, n, m, result, pos;
int count[10], pri;
queue<int> q;

void inp()
{
	cin >> n >> m;

	pos = m;
	for (int i = 0; i < 10; i++) count[i] = 0;

	for (int i = 0; i < n; i++)
	{
		int x;
		cin >> x;
		q.push(x);
		count[x]++;
		if (i == m) pri = x;
	}
}

void printer(int number)
{
	while (count[number] > 0)
	{
		if (q.front() == number)
		{
			q.pop();
			count[number]--;
			result++;
		}
		else
		{
			q.push(q.front());
			q.pop();
		}
		if (pos == 0) pos = q.size()-1;
		else pos--;
	}
}

void process()
{
	result = 0;
	for (int i = 9; i >= pri+1; i--)
		if (count[i] > 0) printer(i);

	for (int i = 0; i <= pos; i++)
	{
		int x = q.front();
		q.pop();
		if (x == pri) result++;
	}
}

void out()
{
	cout << result << endl;
}

int main()
{
	cin >> ntest;
	for (int i = 0; i < ntest; i++)
	{
		inp();
		process();
		out();
	}
	return 0;	
}
